package com.samapp.indent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.dragon.httpclient_util.HttpClientThread;
import com.dragon.httpclient_util.HttpResultHandle;
import com.dragon.httpclient_util.JsonUtils;
import com.google.gson.reflect.TypeToken;
import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.entity.DetectionItem;
import com.samapp.utils.json.GsonUtil;

public class DetectionItemActivity extends Activity implements OnClickListener {

	/** 返回按钮 */
	private Button btnBack = null;
	/** 项目名 */
	private EditText editTextItemName = null;
	private String itemName;
	/** QS编码 */
	private EditText editTextQScode = null;
	private String qsCode;
	/** SC编码 */
	private EditText editTextSCcode = null;
	private String scCode;
	/** 产品标准名称 */
	private EditText editTextCriteriaName = null;
	/** 产品标准号 */
	private EditText editTextCriteriaNumber = null;
	private String criteriaName;
	private String criteriaNumber;
	/** 提交 */
	private Button btnSubmit = null;
	/** 是否发证检测 */
	private RadioButton rBtnIsfazheng_true = null;
	private String isFaZheng;
	/** 是否监督检测 */
	private RadioButton rBtnIsjiandu_true = null;
	private String isJiandu;
	/** 是否出厂检测 */
	private RadioButton rBtnIschuchang_true = null;
	private String isChuchang;

	private String fromActivity = null;

	private MyApplication myApplication = MyApplication.getApplicationInstance();

	private String detectionInfo;
	public static final int MSG_DETECTION_ITEM = 0x190;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == MSG_DETECTION_ITEM) {
				Toast.makeText(DetectionItemActivity.this, detectionInfo, Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detection_item);
		init();
	}

	private void init() {
		fromActivity = getIntent().getStringExtra("intentMark");
		myApplication.listDetectionItem.clear();
		initView();
		initListener();
	}

	private void initView() {
		btnBack = (Button) findViewById(R.id.btn_back);
		editTextItemName = (EditText) findViewById(R.id.itemname);
		editTextQScode = (EditText) findViewById(R.id.qscode);
		editTextSCcode = (EditText) findViewById(R.id.sccode);
		editTextCriteriaName = (EditText) findViewById(R.id.criteriaName);
		editTextCriteriaNumber = (EditText) findViewById(R.id.criteriaNumber);
		rBtnIsfazheng_true = (RadioButton) findViewById(R.id.isfazheng_true);
		rBtnIsjiandu_true = (RadioButton) findViewById(R.id.isjiandu_true);
		rBtnIschuchang_true = (RadioButton) findViewById(R.id.ischuchang_true);
		btnSubmit = (Button) findViewById(R.id.btn_submit);
	}

	private void initListener() {
		btnBack.setOnClickListener(this);
		btnSubmit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_submit:
			setData();
			http();
			break;
		}
	}

	private void setData() {
		itemName = editTextItemName.getText().toString().trim();
		qsCode = editTextQScode.getText().toString().trim();
		scCode = editTextSCcode.getText().toString().trim();
		criteriaName = editTextCriteriaName.getText().toString().trim();
		criteriaNumber = editTextCriteriaNumber.getText().toString().trim();
		if (rBtnIsfazheng_true.isChecked()) {
			isFaZheng = "true";
		} else {
			isFaZheng = "false";
		}
		if (rBtnIsjiandu_true.isChecked()) {
			isJiandu = "true";
		} else {
			isJiandu = "false";
		}
		if (rBtnIschuchang_true.isChecked()) {
			isChuchang = "true";
		} else {
			isChuchang = "false";
		}
	}

	private void http() {
		String url = myApplication.url + "queryTestItem.htm";
		Map<String, String> myMap = new HashMap<String, String>();
		myMap.put("accessToken", "5C9.yx0S");
		myMap.put("itemName", itemName);
		myMap.put("isFazheng", isFaZheng);
		myMap.put("isJiandu", isJiandu);
		myMap.put("isChuchang", isChuchang);
		myMap.put("qsCode", qsCode);
		myMap.put("scCode", scCode);
		myMap.put("criteriaName", criteriaName);
		myMap.put("criteriaNumber", criteriaNumber);
		HttpClientThread.getmHttpClientThread().post(url, myMap, new HttpResultHandle() {

			@Override
			public void onSuccess(String response) {
				Log.i("Long", response);
				String code = JsonUtils.getValue(response, "code");
				if ("0".equals(code)) {
					myApplication.listDetectionItem = GsonUtil.getGson().fromJson(JsonUtils.getValue(response, "list"), new TypeToken<List<DetectionItem>>() {
					}.getType());
					if (myApplication.listDetectionItem.size() == 0) {
						detectionInfo = "没有查询到此检测项目";
						mHandler.sendEmptyMessage(MSG_DETECTION_ITEM);
					} else {
						detectionInfo = JsonUtils.getValue(response, "msg");
						mHandler.sendEmptyMessage(MSG_DETECTION_ITEM);
						if ("MainActivity".equals(fromActivity)) {
							Intent intent = new Intent(DetectionItemActivity.this, DetectionItemListActivity.class);
							startActivity(intent);
							finish();
						} else if ("AddIndentDetailsActivity".equals(fromActivity)) {
							Intent intent = new Intent();
							setResult(AddIndentDetailsActivity.CODE_DETECTION_ITEM, intent);
							finish();
						}
					}
				} else {
					detectionInfo = JsonUtils.getValue(response, "msg");
					mHandler.sendEmptyMessage(MSG_DETECTION_ITEM);
				}
			}

			@Override
			public void onFailure(int statusCode) {
				detectionInfo = "连接服务器失败，状态码：" + statusCode;
				mHandler.sendEmptyMessage(MSG_DETECTION_ITEM);
			}
		});
	}
}
